package cc.linker.steplink.crm.merchant.controller;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.service.ShopGroupService;
import cc.linker.steplink.crm.merchant.service.acs.MerchantInfoService;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop/group")
public class ShopGroupController {

    @Autowired
    private ShopGroupService shopGroupService;
    @Autowired
    private MerchantInfoService merchantInfoService;
    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 分组列表
     *
     * @param groupType 分组类型 1-商品2-知识付费
     */
    @GetMapping("/list")
    public Object list(@RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
                       @RequestParam(value = "pageSize", defaultValue = "20", required = false) Integer pageSize,
                       @RequestParam(value = "groupType", defaultValue = "1", required = false) Integer groupType,
                       HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        Boolean ifSales = userinfo.getSaler();
        Map<String, Object> map = new HashMap<>();
        if (ifSales != null && ifSales) {
            String adminId = userinfo.getAdminId();
            String customerId = merchantInfoService.getCustomerIdByAdminId(adminId);
            map.put("customerId", customerId);
            map.put("belongId", customerId);
        } else {
            map.put("belongId", orgId);
        }
        map.put("orgId", orgId);
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        map.put("groupType", groupType);
        return shopGroupService.list(map);
    }

    /**
     * 给其他项目用的接口
     *
     * @param orgId     机构id
     * @param adminId   用户id
     * @param groupType 分组类型0-全部1-商品2-知识付费
     */
    @GetMapping("/listAll")
    public List<Map<String, Object>> listAll(String orgId, String adminId, String type, Integer groupType,HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        if (StringUtils.isEmpty(adminId)) {
            adminId = userinfo.getAdminId();
        }
        if (StringUtils.isEmpty(orgId)) {
            orgId = userinfo.getOrgId();
        }
        Boolean ifSales = userinfo.getSaler();
        String customerId;
        if (ifSales != null && ifSales) {
            customerId = merchantInfoService.getCustomerIdByAdminId(adminId);
        } else {
            customerId = orgId;
        }
        if (groupType == null) {
            groupType = 1;
        }
        return shopGroupService.listAll(orgId, customerId, type, groupType);
    }

    /**
     * 删除分组
     *
     * @param groupId 分组id
     */
    @GetMapping("/del")
    public void del(String groupId) {
        shopGroupService.del(groupId);
    }

    /**
     * 新增分组
     *
     * @param groupName 分组名
     * @param groupType 分组类型 1-商品分组2-知识付费分组
     */
    @PostMapping("/insert")
    public void insert(@RequestBody Map<String, Object> map, HttpServletRequest request) throws GlobalException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        map.put("orgId", orgId);
        Boolean ifSales = userinfo.getSaler();
        if (ifSales != null && ifSales) {
            String adminId = userinfo.getAdminId();
            String customerId = merchantInfoService.getCustomerIdByAdminId(adminId);
            map.put("customerId", customerId);
        } else {
            map.put("customerId", orgId);
        }
        shopGroupService.insert(map);
    }

    /**
     * 编辑分组
     *
     * @param groupId   分组id
     * @param groupName 分组名
     */
    @PostMapping("/edit")
    public void edit(@RequestBody Map<String, Object> map,HttpServletRequest request) throws GlobalException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        map.put("orgId", userinfo.getOrgId());
        shopGroupService.edit(map);
    }
}
